<template>
  <div class="container">
    <div class="app-container">
      <el-button type="primary" class="btn-add" size="mini" @click="addPer(0,1)">添加权限</el-button>
      <!-- 放置表格 -->
      <!-- 指定row-key的值是数据中的id，这个id是唯一的 -->
      <el-table :data="list" row-key="id" default-expand-all>
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button v-if="row.type === 1" type="text" size="small" @click="addPer(row.id,2)">添加</el-button>
            <el-button type="text" size="small" @click="editPre(row.id)">编辑</el-button>
            <el-button type="text" size="small" @click="delPer(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :title="showMyTitle" :visible="showDialog" @close="close">

      <el-form ref="perForm" :model="formData" label-width="120px" :rules="rules">
        <el-form-item prop="name" label="权限名称">
          <el-input v-model="formData.name" style="width:80%" />
        </el-form-item>

        <el-form-item prop="code" label="权限标识">
          <el-input v-model="formData.code" style="width:80%" />
        </el-form-item>

        <el-form-item prop="description" label="权限描述">
          <el-input v-model="formData.description" style="width:80%" />
        </el-form-item>

        <el-form-item label="开启" prop="enVisible">
          <el-switch
            v-model="formData.enVisible"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>

        <el-form-item>
          <!-- 想要弹性布局，可以用el-row和el-col标签进行布局 -->
          <el-row type="flex" justify="center">
            <el-col :span="6">
              <el-button type="primary" size="small" @click="btnOk">确定</el-button>
              <el-button size="small" @click="close">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getPermissionList, delPermission, addPermission, updatePermission, getPermissionDetail } from '@/api/permission'
import { transListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showMyTitle: '',
      showDialog: false, // 添加部门按钮弹框
      formData: {
        name: '',
        code: '',
        id: 0,
        pid: 0,
        description: '',
        enVisible: '0'
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      // 拿到的数据需要转换成树形，数据中也是id和pid形式
      this.list = transListToTreeData(await getPermissionList(), 0)
      // console.log(this.list)
    },
    addPer(pid, type) {
      this.showMyTitle = '新增权限'
      this.showDialog = true
      this.formData.pid = pid
      this.formData.type = type
    },
    delPer(id) {
      this.$confirm('是否删除该权限', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delPermission(id)
        this.$message.success('删除成功')
        this.getPermissionList()
      }).catch(() => {
        this.$message.info('已取消删除')
      })
      // 删除后，需要重新获取部门数据，更新视图
      this.getPermissionList()
    },
    async btnOk() {
      this.$refs.perForm.validate().then(() => {
        if (this.formData.id) {
          return updatePermission(this.formData)
        }
        return addPermission(this.formData)
      }).then(() => {
        //  提示消息
        this.$message.success('新增成功')
        this.getPermissionList()
        this.showDialog = false
      })
    },
    async editPre(id) {
      this.showMyTitle = '编辑权限'
      this.formData = await getPermissionDetail(id)
      this.showDialog = true
    },
    close() {
      this.formData = {
        name: '',
        code: '',
        id: 0,
        pid: 0,
        description: '',
        enVisible: '0'
      }
      this.$refs.perForm.resetFields()
      this.showDialog = false
    }
  }
}
</script>

<style scoped>
.btn-add {
  margin: 10px;
}
</style>
